﻿using System.Collections.Generic;
using Ewk.MachineLearning.Classification.Common;
using Ewk.MachineLearning.Classification.Interfaces;

namespace Ewk.MachineLearning.Persistance.Domain
{
    /// <summary>
    /// Represents a message in a <see cref="Conversation"/>.
    /// </summary>
    public class Message :
        TextEntity,
        ILinkedTextEntity
    {
        private ICollection<WordInMessageLink> _wordsInMessage;

        /// <summary>
        /// A link to a collection of <see cref="Word"/> instance that appear in this <see cref="Message"/>.
        /// </summary>
        public virtual ICollection<WordInMessageLink> WordsInMessage
        {
            get { return _wordsInMessage ?? (_wordsInMessage = new List<WordInMessageLink>()); }
            set { _wordsInMessage = value; }
        }

        #region Implemenation of ILinkedTextEntity
        #region Implementation of IEntityEquatable<in ILinkedEntity>

        /// <summary>
        /// Gives an indication if two instances of the same Type can be considered equal
        /// for machinelearning purposes.
        /// </summary>
        /// <param name="other">The entity to compare with.</param>
        /// <returns>True if the instances can be considered equal, otherwise false.</returns>
        bool IEntityEquatable<ILinkedEntity>.EqualsForMachineLearning(ILinkedEntity other)
        {
            var comparer = new TextEntityComparer<ILinkedTextEntity>();
            return comparer.Equals(this, other as ILinkedTextEntity);
        }

        #endregion

        #region Implementation of ILinkedEntity<Message,Word>

        /// <summary>
        /// Gets the links to the other entity
        /// </summary>
        IEnumerable<IFeatureLink> ILinkedEntity.Links
        {
            get { return WordsInMessage; }
        }

        /// <summary>
        /// Adds a link between two entities.
        /// </summary>
        /// <param name="link">The link to add.</param>
        void ILinkedEntity.AddLink(IFeatureLink link)
        {
            WordsInMessage.Add((WordInMessageLink) link);
        }

        #endregion
        #endregion
    }
}